package com.yanqu.road.server.cmd.activity.douluodalu;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在线时，给跨服消息确认
 */
@Cmd(code = Protocol.S_DOU_LUO_DA_LU_PUSH_MSG,desc = "消息通知")
public class DouLuoDaLuOnLinePushCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        DouLuoDaLuProto.DouLuoDaLuOnLinePushMsg pushResp = DouLuoDaLuProto.DouLuoDaLuOnLinePushMsg.parseFrom(packet.getBytes());

        //有活动
        if(ActivityMgr.activityInShowTime(DouLuoDaLuMgr.getActivityInfo())){
            int activityId = DouLuoDaLuMgr.getActivityInfo().getActivityId();
            Map<Integer, List<Long>> typeMap = new HashMap<>();
            for (DouLuoDaLuProto.DouLuoDaLuOnLinePushTemp pushTemp : pushResp.getPushList()) {
                int type = pushTemp.getType();
                List<AbstractMessage.Builder> temp = new ArrayList<>();
                if(type == 1 && pushTemp.hasLeave()){
                    temp.add(pushTemp.getLeave().toBuilder());
                }else if(type == 2 && pushTemp.getDeathCount() > 0){
                    for (DouLuoDaLuProto.DouLuoDaLuDeathInfo deathInfo : pushTemp.getDeathList()) {
                        temp.add(deathInfo.toBuilder());
                    }
                }else if(type == 3){

                }
                if(temp == null){
                    continue;
                }
                List<Long> userList = new ArrayList<>();
                for (long uid : pushTemp.getUserList()) {
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(uid);
                    if(player != null){
                        sendToClient(type, player, temp);
                        userList.add(uid);
                    }
                }
                if(userList.size() > 0){
                    typeMap.put(type, userList);
                }
            }
            //sendToCross(activityId, typeMap);
        }
    }


    public void sendToClient(int type, GamePlayer player, List<AbstractMessage.Builder> msgList){
        int code = 0;
        if(type == 1){
            //DouLuoDaLuMgr.getLogger().debug("-----------U_DOU_LUO_DA_LU_NOTIFY_LEAVE_INFO--------------");
            code = Protocol.U_DOU_LUO_DA_LU_NOTIFY_LEAVE_INFO;
        }else if(type == 2){
            //DouLuoDaLuMgr.getLogger().debug("-----------U_DOU_LUO_DA_LU_NOTIFY_DEATH_INFO--------------");
            code = Protocol.U_DOU_LUO_DA_LU_NOTIFY_DEATH_INFO;
        }

        if(code > 0){
            for (AbstractMessage.Builder message : msgList) {
                player.sendPacket(code, message);
            }
        }
    }

    private void sendToCross(int activityId, Map<Integer, List<Long>> typeMap, int groupId) {
        DouLuoDaLuProto.DouLuoDaLuOnLineACKMsg.Builder ackMsg = DouLuoDaLuProto.DouLuoDaLuOnLineACKMsg.newBuilder();
        ackMsg.setActivityId(activityId);
        for (Map.Entry<Integer, List<Long>> entry : typeMap.entrySet()) {
            DouLuoDaLuProto.DouLuoDaLuOnLineACKTemp.Builder temp = DouLuoDaLuProto.DouLuoDaLuOnLineACKTemp.newBuilder();
            temp.setType(entry.getKey());
            temp.addAllUser(entry.getValue());
            ackMsg.addAck(temp);
        }
        GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_PUSH_MSG_ACK, ackMsg, 0, ackMsg.getActivityId(), groupId);
    }
}
